﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing.Printing;

namespace TARLib
{
    public class Menu : IList<Prodotto>
    {
        public Menu(int menuID, DateTime inizioValidita, DateTime? fineValidita, string nomeEvento, Guid menuVersionGuid, DateTime menuVersionTimeStamp)
        {
            _InternalList = new List<Prodotto>();

            MenuID = menuID;
            InizioValidita = inizioValidita;
            FineValidita = fineValidita;
            NomeEvento = nomeEvento;
            MenuVersionGuid = menuVersionGuid;
            MenuVersionTimeStamp = menuVersionTimeStamp;
        }

        private List<Prodotto> _InternalList;

        public int MenuID { get; set; }
        public DateTime InizioValidita { get; set; }
        public DateTime? FineValidita { get; set; }
        public string NomeEvento { get; set; }
        public Guid MenuVersionGuid { get; set; }
        public DateTime MenuVersionTimeStamp { get; set; }

        public void StampaMenu(ref PrintPageEventArgs e, int codiceBuono)
        {
            int i = 0; // Contatore prodotti
            int row = 0;
            int column = 0;
            float totalLength = e.PageBounds.Width * 0.90f;
            float totalHeight = e.PageBounds.Height * 0.85f;
            float leftMargin = e.PageBounds.Width * 0.05f;
            float topMargin = e.PageBounds.Height * 0.1f;
            float rectanglePDLength = 20;
            int nColumns = 5;
            int nRows = _InternalList.Count / nColumns + 1; // Da calcolare

            e.Graphics.DrawString("TAVOLO", new System.Drawing.Font("Arial Black", 25, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Pixel), System.Drawing.Brushes.Black, new System.Drawing.PointF(30, 15));
            iTextSharp.text.pdf.Barcode128 b = new iTextSharp.text.pdf.Barcode128();
            b.Code = new TARLib.Operazioni.Operazione_Login(codiceBuono).BarcodeString;
            System.Drawing.Image image = b.CreateDrawingImage(System.Drawing.Color.Black, System.Drawing.Color.White);
            e.Graphics.DrawImage(image, e.PageBounds.Width * 0.7f, 10, 150, 50);
            
            // Recupera tutti i punti di distribuzione
            foreach (int pdID in _InternalList.Select(o => o.PuntoDiDistribuzione.PuntoDistribuzioneID).Distinct().OrderBy(o => o))
            {
                column = i % nColumns;
                row = i / nColumns;

                // Disegna rettangolino grigio
                e.Graphics.FillRectangle(System.Drawing.Brushes.LightGray, column * (totalLength / nColumns) + leftMargin, row * (totalHeight / nRows) + topMargin, rectanglePDLength, (totalHeight / nRows));

                foreach (Prodotto p in _InternalList.Where(o => o.PuntoDiDistribuzione.PuntoDistribuzioneID == pdID).OrderBy(o => o.ProdottoID))
                {
                    column = i % nColumns;
                    row = i / nColumns;

                    // Scrivi etichetta prodotto
                    e.Graphics.DrawString(p.NomeBreve, new System.Drawing.Font("Times New Roman", 12, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Pixel), System.Drawing.Brushes.Black, new System.Drawing.PointF(column * (totalLength / nColumns) + rectanglePDLength + 5 + leftMargin, row * (totalHeight / nRows + 0.1f) + topMargin + 5));

                    // Aggiungi rettangolo
                    e.Graphics.DrawRectangle(System.Drawing.Pens.Black, column * (totalLength / nColumns) + leftMargin, row * (totalHeight / nRows) + topMargin, (totalLength / nColumns), (totalHeight / nRows));

                    i++;
                }
            }
        }

        #region IList<Prodotto> Members

        public int IndexOf(Prodotto item)
        {
            return _InternalList.IndexOf(item);
        }

        public void Insert(int index, Prodotto item)
        {
            _InternalList.Insert(index, item);
        }

        public void RemoveAt(int index)
        {
            _InternalList.RemoveAt(index);
        }

        public Prodotto this[int index]
        {
            get
            {
                return _InternalList[index];
            }
            set
            {
                _InternalList[index] = value;
            }
        }

        #endregion

        #region ICollection<Prodotto> Members

        public void Add(Prodotto item)
        {
            _InternalList.Add(item);
        }

        public void Clear()
        {
            _InternalList.Clear();
        }

        public bool Contains(Prodotto item)
        {
            return _InternalList.Contains(item);
        }

        public void CopyTo(Prodotto[] array, int arrayIndex)
        {
            _InternalList.CopyTo(array, arrayIndex);
        }

        public int Count
        {
            get { return _InternalList.Count; }
        }

        public bool IsReadOnly
        {
            get { return ((IList<Prodotto>)_InternalList).IsReadOnly; }
        }

        public bool Remove(Prodotto item)
        {
            return _InternalList.Remove(item);
        }

        #endregion

        #region IEnumerable<Prodotto> Members

        public IEnumerator<Prodotto> GetEnumerator()
        {
            return _InternalList.GetEnumerator();
        }

        #endregion

        #region IEnumerable Members

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            return _InternalList.GetEnumerator();
        }

        #endregion
    }
}
